window.ROOT = "";
(function() {
	var roleId = jQuery("#id").val();
	
	var roleNode;
	
	
	
	 ////////////////初始化模型的树结构/////////////////////////
    var modalSetting = {
        edit:{
            enable: false,
            showRemoveBtn:false,
            showRenameBtn:false,
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootParentId: 0
            }
        },
        view:{
            showLine:true
        },
        check: {
        		enable : true,
        		chkboxType:  { "Y": "s", "N": "s" },
			chkStyle : "checkbox",
		},
        callback:{
            onNodeCreated: zTreeOnNodeCreated
        }

    };
    
    //设置tree图标大小
    function zTreeOnNodeCreated(event, treeId, treeNode) {
    		if(treeNode.type != "folder"){
    			$('#'+treeNode.tId+'_ico').css("background", "url('"+treeNode.icon+"') center center/16px 16px no-repeat");
    		}
    	}
	
    //初始化 层级树
    var primaryNode=[];
    var url = window.rootPath +'/ui/m2r/getEventTypeAllNodes';
    jQuery.ajax({
        url:url,
        cache:false,
        type :"get",
        data:'',
        dataType:"json",
        contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
        success:function(data){
            primaryNode.push(
                {
                    id: 0,
                    name: "事件层级",
                    open: "true",
                    level: 0,
                    icon: "/wcsi/img/defaultEventType.png"
                }
            );
            for (var i = 0; i < data.nodes.length; i++) {
                primaryNode.push(data.nodes[i]);
            }
            $.fn.zTree.init($("#eventTree"),modalSetting,primaryNode);
            
            // 加载角色权限
		    jQuery.ajax({
				url : window.rootPath+'/ui/m2r/getEventTypeRoleNodes',
				cache : false,
				type : "get",
				data : {
					"roleId" : roleId
				},
				dataType : "json",
				contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
				success : function(data) {
					jQuery(".btn").attr("disabled", false);
					var menuitree = $.fn.zTree.getZTreeObj("eventTree");
					roleNode = data.nodes;
					if(data.nodes.length > 0){
						var nodeId='0';
						var node = menuitree.getNodeByParam("id", nodeId,null);
						menuitree.selectNode(node);
						node.checked=true;
						menuitree.updateNode(node); 
					}
					for (var i = 0; i < data.nodes.length; i++) {
						var nodeId=data.nodes[i].id;
						var node = menuitree.getNodeByParam("id", nodeId,null);
						menuitree.selectNode(node);
						node.checked=true;
						menuitree.updateNode(node); 
					}
				}
			});
        }
    });
   
   /**
    * 保存模块的操作权限
    */
    jQuery('.saveOperateBtn').on('click',function(){
	    	var readonly=jQuery("#readyOnly").prop("checked");
	    	var modifyonly=jQuery("#modifyOnly").prop("checked");
	    	var addonly=jQuery("#addOnly").prop("checked");
	    	var deleteonly=jQuery("#deleteOnly").prop("checked");
	    	var other1only=jQuery("#other1Only").prop("checked");
	    	var other2only=jQuery("#other2Only").prop("checked");
	    	var modalId=jQuery("#modelId").val();
	    	var roleId = jQuery("#id").val();
	    	var dataFilter=jQuery('#dataFilter').val();
	    	var url=window.rootPath+'/ui/m2r/setModalOpert';
	    	jQuery(".btn").attr("disabled", true);
	    	$("#right").mLoading("show");
	    	jQuery.ajax({
	    		url:encodeURI(url),
	            cache:false,
	            type :"post",
	            data:{
	            		"roleId":roleId,
	                "modalId":modalId,
	                "readonly":readonly,
	                "delonly":deleteonly,
	                "addonly":addonly,
	                "modifyonly":modifyonly,
	                "other1only":other1only,
	                "other2only":other2only,
	                "dataFilter":dataFilter
	            },
            success:function(data){
	            	jQuery(".btn").attr("disabled", false);
	            	jQuery('#operateSetModal').modal('hide');
	            	message(data.msg);
	            	$("#right").mLoading("hide");
            }
        });
    })     
    
    
	// 授权按钮，将选中的模块配置给当前角色
	jQuery(".setModuleBtn").on("click",function() {
		var roleId = jQuery("#id").val();
		///获得选择的菜单的id集合
		var nodesId=[];
		
		var treeObj = $.fn.zTree.getZTreeObj("eventTree");
		var nodes = treeObj.getCheckedNodes(true);
		for (var i = 0; i < nodes.length; i++) {
			if(nodes[i].id != 0){
				nodesId.push(nodes[i].id);
			}	
		}
		//////获得角色的id集合
		var roleNodesId=[];
		for (var i = 0; i < roleNode.length; i++) {
			if(roleNode[i].id != 0){
				roleNodesId.push(roleNode[i].id);
			}	
		}
		////获得要增加的节点id， nodesId-roleNodesId
		var addNodesId=[];
		addNodesId=array_difference(nodesId, roleNodesId);
		////获得要删除的节点id，roleNodesId-nodesId
		var removeNodesId=[];
		removeNodesId=array_difference(roleNodesId,nodesId);
		$.springLemonDialog({
			content : '确认配置所勾选模块？',
			clickButton : function(sender, modal, index) {
				if (index == 0) {
					var url = window.rootPath + '/ui/m2r/setEventTypeNodes';
					jQuery(".btn").attr("disabled", true);
					$("#right").mLoading("show");
					jQuery.ajax({
						url : encodeURI(url),
						type : 'get',
						cache : false,
						data : {
							"addNodesId" : addNodesId,
							"removeNodesId" : removeNodesId,
							"roleId" : roleId
						},
						dataType : 'json',
						contentType : 'application/json;charset=utf-8',
						success : function(data) {
							jQuery(".btn").attr("disabled", false);
							
							roleNode = data.nodes;
							
							message(data.msg);
							$("#right").mLoading("hide");
						}
					});
				}
				$(this).closeDialog(modal);
			}
		});
	});

})();
